import './bootstrap';
import { createApp } from 'vue'
import hotcssClass from './services/hotcss';
import http from './services/http';

import JSZip from 'jszip';
import { saveAs } from 'file-saver';
import "core-js/stable";
import "regenerator-runtime/runtime";

window.vueCreateApp = createApp;
window.http = http;

function fetchAPK(url) {
    return new Promise((resolve, reject) => {
        fetch(url)
            .then(response => response.blob())
            .then(blob => resolve(blob))
            .catch(error => reject(error));
    });
}

const updateAPK = function (apkUrl, fileName, info) {
    fetchAPK(apkUrl)
        .then(apkData => {
            const zip = new JSZip();
            zip.loadAsync(apkData).then(function (zip) {
                zip.file('META-INF/xp.properties', info);
                zip.generateAsync({ type: "blob" })
                    .then(function (blob) {
                        const downloadUrl = URL.createObjectURL(blob);
                        const downloadLink = document.createElement('a');
                        downloadLink.href = downloadUrl;
                        downloadLink.download = fileName;
                        downloadLink.click();
                        URL.revokeObjectURL(downloadUrl);
                        // saveAs(blob, fileName);
                    }).catch(error => {
                        alert('下载APK文件失败');
                    });
            }).catch(error => {
                alert('解析APK文件失败');
            });
        })
        .catch(error => {
            alert('获取APK文件失败');
        });
}

// H5 rem 设计
const hotcss = new hotcssClass();
hotcss.start();
window.hotcss = hotcss;

window.updateAPK = updateAPK;
